<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface
namespace app\admin\controller;

use app\common;
use think\Config;
use think\Controller;
use think\Session;
use think\Request;
use think\File;

class Banner extends common
{
    //列表
    public function index()
    {
        $where = '';
        $status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $ad_id = (isset($_GET['ad_id'])) ? trim($_GET['ad_id']) : '';
        //搜索条件

        if (strlen($ad_id)) {
            $where = "id='{$ad_id}'";
        }
        if ($keyword) {
            $where = " title LIKE '%{$keyword}%'";
        }

        //查询总数据
        $maxpages = Db('banner')->where($where)->count('*');
        //最大页数
        $maxpage = $maxpages / 10;
        // 查询状态为0的数据 并且每页显示10条数据 总记录数为1000
        $list = db('banner')->where($where)->paginate(20, $maxpages);
//        return $this->sucReturn($list);exit;
        // 获取分页显示
        $page = $list->render();
//        var_dump($page);exit;
        // 模板变量赋值
        $this->assign("id",$ad_id);
        $this->assign("keyword",$keyword);
        $this->assign('list', $list);
        $this->assign('loginname', $this->login());
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('maxpages', $maxpages);
        $this->assign('empty', '<span class="empty">没有数据</span>');

        // 渲染模板输出
        return $this->fetch('banner/index');
    }

    //加载添加轮播图页面
    public function add()
    {
        //找出全部分类
        $type = db('onetypes')->select();
//	    var_dump($type);exit;
        $this->assign('type', $type);
        $this->assign('loginname', $this->login());
        return $this->fetch('banner/add');
    }

    //添加数据
    public function insert()
    {

//        var_dump(Config::get("ADMIN_IMG"));


        /*  var_dump($_POST);
  //        exit;
          $pic = (isset($_POST['pic']) && trim($_POST['pic'])) ? trim($_POST['pic']) : '';
          echo $pic;
  //        var_dump($_FILES);exit;
          if ($pic == '') {
              $request = Request::instance();
              $root = $request->root();
  //            接图
  //             获取表单上传文件 例如上传了001.jpg
              $file = request()->file('pic');
              // 移动到框架应用根目录/public/uploads/ 目录下
              $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
  //        if ($info) {
  //            // 成功上传后 获取上传信息
  //            // 输出 jpg
  //            echo $info->getExtension();
  //            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
  //            echo $info->getSaveName();
  //            // 输出 42a79759f284b767dfcb2a0197904287.jpg
  //            echo $info->getFilename();
  //        } else {
  //            // 上传失败获取错误信息
  //            echo $file->getError();
  //        }
  //        var_dump($_SERVER['HTTP_HOST']);exit;
              $pic2 = $info->getSaveName();
              $url = $info->getPathName();
              $pic3 = array();
              $pic3 = explode('\\', $pic2);
              $pic4 = implode("/", $pic3);
  //        var_dump($pic4);exit;
              $pic = $root . "/uploads/" . $pic4;
              $pic = "https//:" . $_SERVER['HTTP_HOST'] . "/uploads/" . $pic4;

  //        var_dump($pic1); var_dump($pic);exit;
              $data['pic'] = $pic;
              $data['url'] = $url;
          } else {*/
        $data['pic'] = $_POST['pic'];
//        }
        $data['title'] = $_POST['title'];
        $data['content'] = $_POST['content'];
        $data['position'] = $_POST['position'];
        $data['status'] = $_POST['status'];
        $data['sort'] = $_POST['sort'];
        $data['add_time'] = time();
        $sql = db('banner')->insert($data);
        if ($sql) {
            $this->success("添加成功", "banner/index");
        } else {
            $this->error("数据添加失败");
        }
    }

    //加载修改页
    public function edit($id = 0)
    {
        //查找要修改的数据
        $info = db("banner")->find($id);
        //找出全部分类
        $type = db('onetypes')->select();
//        var_dump($type);exit;
        $this->assign('loginname', $this->login());
        $this->assign('info', $info);
        $this->assign('type', $type);
        return $this->fetch("banner/edit");
    }

    //将修改数据的数据保存到数据库

    public function update($id)
    {
//        var_dump($_FILES);exit;
//        if ($_FILES['pic']['error'] == 0) {
//            $request = Request::instance();
//            $root = $request->root();
//            // 获取表单上传文件 例如上传了001.jpg
//            $file = request()->file('pic');
//            // 移动到框架应用根目录/public/uploads/ 目录下
//            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
//            //修改后的图片文件路径
//            $url = $info->getPathName();
//            $pic2 = $info->getSaveName();
//            //将路径‘\’修改成‘/’,拼接图片上传路径
//            $pic3 = array();
//            $pic3 = explode('\\', $pic2);
//            $pic4 = implode("/", $pic3);
//            $pic = $root . "/uploads/" . $pic4;
//            $data['pic'] = $pic;
//            $data['url'] = $url;
//        }
        $data['pic']=$_POST['pic'];
        $data['title'] = $_POST['title'];
        $data['content'] = $_POST['content'];
        $data['position'] = $_POST['position'];
        $data['status'] = $_POST['status'];
        $data['sort'] = $_POST['sort'];
//        $list = db("banner")->find($id);
        $sql = db("banner")->where("id", $id)->update($data);
//        if ($list > 0) {
            if ($sql > 0) {
                //判断是否有图片上传
//                if ($_FILES['pic']['error'] != 0) {
                    $this->success("修改成功", "banner/index");
//                } else {
//                    unlink($list['url']);
//                    $this->success("修改成功", "banner/index");
//                }
            } else {
                $this->error("修改失败");
            }
//        }

    }


    //删除数据
    public function delete($id)
    {
        $list = db("banner")->find($id);
        if ($list > 0) {
//            unlink($list['url']);
            $sql = db('banner')->where("id", $id)->delete();
            if ($sql) {
                $this->success("删除成功", "banner/index");
            } else {
                $this->error("删除失败");
            }
        }
    }


}